<template>
  <div>
    <category-nav-bar class="line" ref="bar" />

    <div class="category_main">
      <div class="left">
        <category-left-bar
          v-for="(tag, index) in tags"
          :key="index"
          :tag="tag"
          @doneCategory="doneCategory"
          :index="index"
          :isEnd="isEnd(index)"
          :ref="'category' + index"
          @loadCategory="loadCategory"
        />
      </div>
      <div class="right">
        <tab-control
          :titles="titles"
          @tabClick="tabClick"
          ref="tab2"
          v-show="ceiling"
        />
        <scroll
          class="goodCategory"
          ref="goods"
          @ifBottom="ifBottom"
          :topPot="ceilingPot"
          v-if="goods"
          style="position:relative;"
        >
          <div v-show="this.categoryViews" class="good">
            <category-right-bar
              v-for="(view, index) in categoryViews[chooseCategory]"
              :key="index"
              :view="view"
              @doneView="done"
            />
          </div>

          <tab-control
            :titles="titles"
            @tabClick="tabClick"
            ref="tab1"
            v-show="!ceiling"
          />
          <good-list
            v-if="type"
            :goods="CurrentGoods"
            class="specific"
            @loadGood="done"
          />
        </scroll>
      </div>
    </div>
  </div>
</template>
<script scoped>
import Scroll from "components/common/Scroll/Scroll";

import CategoryNavBar from "./childCpn/CategoryNavBar";
import CategoryLeftBar from "./childCpn/CategoryLeftBar";
import CategoryRightBar from "./childCpn/CategoryRightBar";

import GoodList from "components/content/Goods/GoodList";
import TabControl from "components/content/Tab/TabControl";

import { debounce } from "common/utils";
import { getCategoryGoods, getCategoryGoodsDetail } from "network/category";

export default {
  name: "Category",
  data() {
    return {
      tags: [],
      chooseCategory: 0,
      categoryViews: [],
      goods: {
        pop: [],
        new: [],
        sell: [],
      },
      type: null,
      done: null,
      loadDone: false, //识图加载完成后再加载tabControl
      titles: ["流行", "新款", "精选"],
      types: ["pop", "new", "sell"],
      ifLowChrome: false,
      ceiling: false,
      ceilingPot: 0,
    };
  },
  components: {
    Scroll,
    CategoryNavBar,
    CategoryLeftBar,
    CategoryRightBar,
    GoodList,
    TabControl,
  },
  created() {
    //防抖doneView
    this.done = debounce(this.doneView);
    //获取分类详情
    getCategoryGoods().then((res) => {
      ;
      for (let c of res.data.data) {
        this.tags.push(c.title);
        this.categoryViews.push(JSON.parse(c.list));
      }
      if (this.$refs.goods) this.$refs.goods.BackTop();
      this.loadType();
    });
  },

  activated() {
    this.$refs.goods?.refresh();
    this.ceiling = this.$refs?.goods.getY() > this.ceilingPot;
  },

  methods: {
    //是否可以显示backtop
    ifBottom(tag = false) {
      this.IsBottom = tag;
      this.ceiling = tag;
    },

    //初始化设置默认值
    doneCategory() {
      this.$refs["category" + 0][0].choosed = true;
      this.type = "pop";
    },

    //获取被点击的子组件index设置对应的category
    loadCategory(index) {
      this.chooseCategory = index;
    },

    //重新计算bs高度
    doneView() {
      this.loadDone = true;
      this.$refs.goods?.refresh();
    },

    //tab点击切换商品视图
    tabClick(index) {
      this.$refs.tab1.currentIndex = this.$refs.tab2.currentIndex = index;
      this.type = this.types[index];
      this.$refs.goods.scrollTo(-this.ceilingPot);
    },

    //加载type商品信息
    loadType() {
      this.$refs.goods.scrollTo(0);
      const popT = getCategoryGoodsDetail(this.chooseCategory + 1, "pop");
      const newT = getCategoryGoodsDetail(this.chooseCategory + 1, "new");
      const sellT = getCategoryGoodsDetail(this.chooseCategory + 1, "sell");
      Promise.all([popT, newT, sellT])
        .then((res) => {
          this.goods["pop"] = res[0].data.data;
          this.goods["new"] = res[1].data.data;
          this.goods["sell"] = res[2].data.data;
          this.$nextTick(() => {
            //计算吸顶条件高度
            this.ceilingPot = this.$refs.tab1.$el.offsetTop;
          });
        })
        .catch();
    },
    isEnd(index) {
      return this.titles.length - 1 == index;
    },
  },

  activated() {},
  computed: {
    //返回所点标题对应商品
    CurrentGoods() {
      return this.goods[this.type];
    },
  },
  watch: {
    //当catetory改变时设置对应的样式
    chooseCategory(newV, oldV) {
      this.$refs["category" + oldV][0].choosed = false;
      this.$refs["category" + newV][0].choosed = true;
      this.loadType();
      this.type = "pop";
    },
  },
};
</script>
<style scoped>
.category_main {
  width: 100vw;
  display: flex;
  height: 85.9vh;
  background-color: white;
  overflow: scroll;
  flex-direction: row;
}

.left {
  width: 25%;
  background-color: rgb(240, 240, 240);
  height: 85.9vh;
  position: fixed;
  z-index: 1;
  overflow: scroll;
}

.right {
  margin-left: 25%;
  width: 75%;
  background-color: white;
  height: 85.9vh;
  position: fixed;
  z-index: 1;
  overflow: scroll;
}

.goodCategory {
  height: 85.9vh;
  overflow-x: hidden;
  overflow-y: hidden;
}

.specific {
  width: 100%;
}

.good {
  display: flex;
  width: 100%;
  flex-flow: row wrap;
  flex: 1;
  gap: 10px;
  padding: 10px;
}

.reload {
  position: fixed;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  z-index: 1;
}

.line {
  border-bottom: 1px solid rgb(229, 229, 229);
}
</style>
